Access specifiers:- Access specifier provide a facility to restricted another class to inherit the property of parents class. Its also provide a path to control the visibility of members like classes, variables and methods. Access specifiers is used for classes, constructor, variable, and method.
There are four types of access specifiers:-
1.public
2.private
3.protected
4.unspecified
1.public:-In public access specifier we can assigned public specifier to different elements like classes, method, constructor and variable.
class ClassName{Public:Int a;}int main(){ClassName obj;Obj.a=10; //Allowed}
2.private:-In private access specifiers we can not assign private specifier to any class. its only assign to variable ,method and constructor. In other words we can not create private class.
Class ClassName{Protected:Int a;};Int main(){ClassName obj;Obj.a=20; // Not allowed, gives compiler error}
Protected access specifiers can be also used for constructor, variable and method.
class ClassName{Private:Int c;};Int main(){ClassName obj;Obj.c=10; //NotAllowed,gives compiler error}
4.Unspecified:-Its same as default. When we can not use any access specifiers,then its categorized as a unspecified. All classes, method, constructor and variable that’s can not be specified with any access specifiers is fall in this category.
class ClassName{Int a;};int main(){ClassName obj;Obj.a=15;}
Leave Comment